package com.dididoctor.patient.Activity.Diagnosis;

import android.content.Context;

import com.dididoctor.patient.Activity.ConstantValue;
import com.dididoctor.patient.Activity.Usercentre.Record.Cases.CasesDetail.Advice;
import com.dididoctor.patient.Bean.Token;
import com.dididoctor.patient.Http.MyHttpResponseHandler;
import com.dididoctor.patient.MV.BasePresenter;
import com.dididoctor.patient.MV.BusinessClient;
import com.dididoctor.patient.MV.Response;
import com.dididoctor.patient.Utils.Util;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/4/16.
 */
public class DiagnosisPresenter extends BasePresenter {

    private DiagnosisView view;

    public DiagnosisPresenter(Context context, DiagnosisView view) {
        super(context, view);
        this.view = view;
    }

    public void getDiagnosis(String recId) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        params.put("recId", recId);
        BusinessClient.post(ConstantValue.medicalview, params, new MyHttpResponseHandler() {
            private List<Map<String, String>> advicesmaps = new ArrayList<Map<String, String>>();
            private List<Advice> advices = new ArrayList<Advice>();

            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    Diagnosis diagnosis = new Diagnosis();
                    diagnosis.setRecId(response.getString("recId"));
                    diagnosis.setHosName(response.getString("hosName"));
                    diagnosis.setClinicDate(response.getString("clinicDate"));
                    diagnosis.setDctName(response.getString("dctName"));
                    diagnosis.setDescr(response.getString("descr"));
                    diagnosis.setResult(response.getString("result"));
                    //医生建议
                    advicesmaps = response.getListData("advices");
                    for (Map<String, String> map : advicesmaps) {
                        Advice advice = new Advice();
                        advice.setType(Util.toString(map.get("type")));
                        advice.setContent(Util.toString(map.get("content")));
                        advices.add(advice);
                    }
                    view.getDiagnosis(diagnosis, advices);
                } else {
                    view.showToastMessage(response.getMsg());
                    view.getDiagnsisfail();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.getDiagnsisfail();
            }
        });
    }
}
